$(function()
{
    $("ul.img-item li").mtolay();
    $('.page-switcher').hover(function(){$(this).removeClass('opy0').addClass('opy5')}, function(){$(this).removeClass('opy5').addClass('opy0')});
    $(".img-item:first img").lazyload({effect: "fadeIn"});  //lazyload

    //showDialog('登录成功！', 'confirm', 'aaa安安', function(){alert(1)});
});

function _(id){
    return !id ? null : document.getElementById(id);
}

function isUndefined(variable){
    return !!typeof variable == 'undefined';
}

function mtLoading(w){
    var w = !isUndefined(w) && w ? w : '加载中...';
    $('#append_parent').append('<div id="ajxwaitid" class="pa">' + w + '</div>');
}


var showDialogST = null;
function showDialog(msg, mode, tile, func, funccancel, tip, confirmtxt, canceltxt, closetime, locationtime) {
    clearTimeout(showDialogST);
    tip  = isUndefined(tip) ? '' : tip;
    mode = isUndefined(mode) ? 'alert' : mode;  //['confirm', 'notice', 'right']
    var menuid = 'mt_dialog';
    if(_(menuid)){
        mtHide(menuid);
    }
    closetime = isUndefined(closetime) ? '' : closetime;
    closefunc = function () {
        if(typeof func == 'function') func();
        else eval(func);
        mtHide(menuid);
    };
    var cfmtxt = '确定';
    if(closetime) {
        tip = closetime + ' 秒后窗口关闭';
        showDialogST = setTimeout(closefunc, closetime * 1000);
    }
    locationtime = isUndefined(locationtime) ? '' : locationtime;
    if(locationtime) {
        tip = locationtime + ' 秒后页面跳转';
        showDialogST = setTimeout(closefunc, locationtime * 1000);
        cfmtxt = '立即跳转';
    }
    cfmtxt = confirmtxt ? confirmtxt : cfmtxt;
    canceltxt  = canceltxt ? canceltxt : '取消';

    $('#append_parent').append('<div class="mtmenu" id="' + menuid + '" style="display:none;"></div>');
    var et = ' style="cursor:move" ';
        et+= 'onmousedown="mtDrag(_(\'' + menuid + '\'), event, 1)" ondblclick="mtHide(\'' + menuid + '\')"';
    var hidedom = !$.browser.msie ? '<style>object{visibility:hidden;}</style>' : '';
    var s = hidedom
            + '<table cellpadding="0" cellspacing="0">'
            + '<tr><td class="t_l"></td><td class="t_c"' + et + '></td><td class="t_r"></td></tr>'
            + '<tr><td class="m_l"' + et + '></td><td class="m_c">'
            + '<h3 class="fn p10 pt5"' + et + '>'
            + '<a class="fr mr5" href="javascript:void(0);" id="' + menuid + '_close" title="关闭" onclick="mtHide(\'' + menuid + '\')">x</a>'
            + '<p>' + (tile ? tile : '提示信息') + '</p></h3>';

    s += '<div class="p10 pt5 w350 dialog-' + mode + '"><p>' + msg + '</p></div>';
    s += '<div class="p10 bc-eb tr">' + (tip ? '<span class="fl c-9">' + tip + '</span>' : '');
    s += '<input class="btn mr5 pl10 pr10 h26" id="' + menuid + '_submit" value="' + cfmtxt + '" type="button" />';
    s += mode == 'confirm' ? '<input class="btn mr5 pl10 pr10 h26" id="' + menuid + '_cancel" value="' + canceltxt + '" onclick="mtHide(\'' + menuid + '\')" type="button" />' : '';
    s += '</div>';

    s += '</td><td class="m_r"' + et + '></td></tr><tr><td class="b_l"></td><td class="b_c"' + et + '></td><td class="b_r"></td></tr></table>';
    $('#' + menuid).html(s);
    if(_('mt_' + menuid + '_submit'))
    {
        _('mt_' + menuid + '_submit').onclick = function() {
            if(typeof func == 'function'){
                func();
            }
            else{
                eval(func);
            }
            mtHide(menuid);
        };
    }
    if(_('mt_' + menuid + '_cancel')) {
        _('mt_' + menuid + '_cancel').onclick = function() {
            if(typeof funccancel == 'function') funccancel();
            else eval(funccancel);
            mtHide(menuid, 'dialog');
        };
        _('mt_' + menuid + '_close').onclick = _('mt_' + menuid + '_cancel').onclick;
    }
    var v = {'menuid':menuid, zindex:900};
    mtMenu(v);
}

function mtWindow(k, url, mode, data, cache) {
    mode = isUndefined(mode) ? 'get' : mode;
    cache = isUndefined(cache) ? 1 : cache;
    data = isUndefined(data) ? null : data;
    var menuid = 'mt_' + k;
    var menuObj = _(menuid);
    var drag = null;
    var hidedom = !$.browser.msie ? '<style>object{visibility:hidden;}</style>' : '';

    var fetchContent = function() {
                            url += cache == -1 ? '&t='+(+ new Date()) : '';
                            var mode = {url:url, type:mode, data: data};
                            mtAjx(mode, 'mtbcontent_' + k, function(){show();});
                        };
    var show = function() {
                    mtHide('mt_dialog');
                    var v = {'menuid':menuid, zindex:900};
                    mtMenu(v);
                };
    if(!menuObj) {
        $('#append_parent').append('<div class="mtmenu" id="' + menuid + '" style="display:none;"></div>');
        var et = ' style="cursor:move" ';
        et+= 'onmousedown="mtDrag(_(\'' + menuid + '\'), event, 1)" ondblclick="mtHide(\'' + menuid + '\')"';

        var m_html = hidedom
                + '<table cellpadding="0" cellspacing="0">'
                + '<tr><td class="t_l"></td><td class="t_c"' + et + '></td><td class="t_r"></td></tr>'
                + '<tr><td class="m_l"' + et + ')"></td><td class="m_c" id="mtbcontent_' + k + '">'
                + '</td><td class="m_r"' + et + '"></td></tr>'
                + '<tr><td class="b_l"></td><td class="b_c"' + et + '></td><td class="b_r"></td></tr>'
                + '</table>';
        $('#' + menuid).html(m_html);
        fetchContent();
    } else {
        show();
    }
    mtGc();
}

function mtAjx(mode, showid, recall, wait) {
    wait != -1 ? mtLoading() : '';
    $.ajax({
        type: mode.type,
        url: mode.url,
        data: mode.data,
        error : function(){
            wait != -1 ? $('#ajxwaitid').remove() : '';
            $('#' + showid).html('请求失败！请稍后重试。');
            if(recall && typeof recall == 'function') {
                recall();
            } else if(recall) {
                eval(recall);
            }
        },
        success: function(data){
            wait != -1 ? $('#ajxwaitid').remove() : '';
            $('#' + showid).html(data);
            if(recall && typeof recall == 'function') {
                recall();
            } else if(recall) {
                eval(recall);
            }
        }
    });
}

function mtHide(mid){
    if(t = $('#' + mid)){
        t.remove();
    }
    if(c = $('#' + mid + '_cover'))
    {
        c.remove();
    }
}

function mtMenu(v){
    var mtcover = function(){
        var h = $('html').outerHeight(true);
        var t = '<div id="' + v.menuid + '_cover" class="mtcover opy5" style="height: ' + h + 'px;z-index:' + v.zindex + '"></div>';
        $('#append_parent').append(t);
    };
    mtcover();
    var t = $('#' + v.menuid);
    var top = $(window).height()/1.5 - parseInt(t.css('height')),
        left = ($(window).width()- parseInt(t.css('width'))) / 2;
    if(top <0){top = 50}
    t.css({'z-index':v.zindex+1, top:top+'px', left:left+'px'}).show();
}

var mtdragDisabled = false;
var mtdrag =[];
function mtDrag(menuObj, e, op) {
    e = e ? e : window.event;
    if(op == 1) {
        if(mtdragDisabled) {
            return;
        }
        mtdrag['drag'] = [e.clientX, e.clientY];
        mtdrag['drag'][2] = parseInt(menuObj.style.left);
        mtdrag['drag'][3] = parseInt(menuObj.style.top);
        document.onmousemove = function(e) {try{mtDrag(menuObj, e, 2);}catch(err){}};
        document.onmouseup = function(e) {try{mtDrag(menuObj, e, 3);}catch(err){}};
        mtGc(e);
    }else if(op == 2 && mtdrag['drag'][0]) {
        var exe = [e.clientX, e.clientY];
        menuObj.style.left = (mtdrag['drag'][2] + exe[0] - mtdrag['drag'][0]) + 'px';
        menuObj.style.top = (mtdrag['drag'][3] + exe[1] - mtdrag['drag'][1]) + 'px';
        mtGc(e);
    }else if(op == 3) {
        mtdrag['drag'] = [];
        document.onmousemove = null;
        document.onmouseup = null;
    }
}

function mtGc(event) {
    e = event ? event : window.event;
    if(!e){ return null;}
    if(e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
    if(e.stopPropagation) {
        e.stopPropagation();
    } else {
        e.cancelBubble = true;
    }
    return e;
}